package com.orange.wwwapi.controller.order;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.R;
import com.orange.core.pojo.entity.order.CreditOrder;
import com.orange.core.pojo.req.order.CreditOrderPageReq;
import com.orange.core.pojo.req.order.CreditOrderRefundReq;
import com.orange.core.pojo.req.order.CreditShopCartExchangeReq;
import com.orange.core.service.order.CreditOrderService;
import com.orange.core.util.PrincipalUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 积分订单
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/creditOrder")
public class CreditOrderController {

    private final CreditOrderService creditOrderService;

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public R<Page<CreditOrder>> page(@RequestBody PageReqDto<CreditOrderPageReq> req) {
        if (req.getCondition() == null) {
            req.setCondition(new CreditOrderPageReq());
        }
        req.getCondition().setUserId(PrincipalUtil.getId());
        return R.ok(creditOrderService.page(req));
    }

    /**
     * 详情
     */
    @GetMapping("/info/{id}")
    public R<CreditOrder> info(@PathVariable("id") int id) {
        return R.ok(creditOrderService.info(id));
    }

    /**
     * 兑换积分商品
     *
     * @return 积分订单id
     */
    @PostMapping("/exchange")
    public R<Integer> exchange(@RequestBody @Validated CreditShopCartExchangeReq req) {
        return R.ok(creditOrderService.exchange(req));
    }

    /**
     * 申请退款
     */
    @PostMapping("/refund/apply")
    public R<Boolean> applyRefund(@RequestBody @Validated CreditOrderRefundReq req) {
        creditOrderService.applyRefund(req);
        return R.ok(true);
    }
}
